package com.danicsoft.daide.command;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.danicsoft.daide.token.StandardToken;
import com.danicsoft.daide.token.TokenGroup;
import com.danicsoft.daide.token.TokenMap;


public class SupplyOwnershipCommand extends QueryableCommand {

	private Map<StandardToken, List<StandardToken>> ownership;

	public SupplyOwnershipCommand(TokenGroup tokens) throws ParseException {
		super(tokens);
	}

	public SupplyOwnershipCommand() {
		super();
	}

	/**
	 * Gets the number of supply centres that a given power owns.
	 * 
	 * @param powerFor
	 *        The power to check the supply centre count for.
	 * @return The amount of supply centres belonging to a given power.
	 */
	public int getSupplyCentreCount(StandardToken powerFor) {
		try {
			return ownership.get(powerFor).size();
		}
		catch (NullPointerException e) {
			return 0;
		}
	}

	@Override
	protected void doNonQueryIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		ownership = tokenIO.doHashOnFirstToken(tokens, ownership, TokenMap.powers, TokenMap.provinces);

	}

	@Override
	public byte getType() {
		return TokenMap.Commands.SCO;
	}

	@Override
	public String toString() {
		return "Supply Centre ownership command";
	}

	@Override
	public void initialize() {
		ownership = new HashMap<StandardToken, List<StandardToken>>();
		super.initialize();
	}

}
